package sample.client.view.gxt;

import java.util.List;

import sample.client.CMModel;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.sencha.gxt.cell.core.client.SimpleSafeHtmlCell;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.ListView;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.StoreFilterField;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent.SelectionChangedHandler;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class FilePanel extends FramedPanel
{
  interface DocTemplate extends XTemplates
  {
    // @XTemplate("<div><span>{name}</span></div>")      // INTERNAL
    @XTemplate(source = "doc.html")
    // EXTERNAL
    SafeHtml render(Document doc, DocumentLooks.Style style);
  }

  public static DocTemplate template = GWT.create(DocTemplate.class);

  private CMModel model;

  private ListStore<Document> store;

  DocumentLooks appearance = DocumentLooks.create();

  private StoreFilterField<Document> fileFilterField;

  private ListView<Document, Document> fileListView;

  private TextButton editButton = new TextButton("Edit");

  private FormPanel fakeForm = new FormPanel();

  private ListView<Document, Document> createListView() {
    store = new ListStore<Document>(Document.props.key());

    ListView<Document, Document> retval =
        new ListView<Document, Document>(store,
            new IdentityValueProvider<Document>(), appearance);

    retval.setCell(getCell());

    retval.getSelectionModel().addSelectionChangedHandler(
        new SelectionChangedHandler<Document>() {
          @Override
          public void onSelectionChanged(SelectionChangedEvent<Document> event)
          {
            List<Document> docList = event.getSelection();
            if (docList.size() > 0) {
              Document doc = docList.get(0);
              LayoutPanel l = new LayoutPanel();
              l.add(new Hidden("clientCode", model.getClientCodeText()));
              l.add(new Hidden("docPath", doc.getPath()));
              l.add(new Hidden("docName", doc.getName()));
              fakeForm.setWidget(l);
              fakeForm.setAction("edit");
              editButton.setEnabled(true);
            }
            else {
              editButton.setEnabled(false);
            }
          }
        });
    editButton.setEnabled(false);
    editButton.addSelectHandler(new SelectHandler() {
      @Override
      public void onSelect(SelectEvent event) {
        fakeForm.submit();
      }
    });

    return retval;
  }

  public ListView<Document, Document> getFileListView() {
    return fileListView;
  }

  public static Cell<Document> getCell() {
    Cell<Document> cell =
        new SimpleSafeHtmlCell<Document>(
            new AbstractSafeHtmlRenderer<Document>() {
              @Override
              public SafeHtml render(Document doc) {
                DocumentLooks.style.ensureInjected();
                return template.render(doc, DocumentLooks.style);

              }
            });
    return cell;
  }

  public ListStore<Document> getStore() {
    return store;
  }

  public FilePanel(final CMModel model) {
    this.model = model;
    setHeadingText("Results");

    addTool(new ToolButton(ToolButton.REFRESH, new SelectHandler() {
      @Override
      public void onSelect(SelectEvent event) {
        model.onClientNameChanged();
      }
    }));

    fileListView = createListView();
    fileFilterField = new StoreFilterField<Document>() {
      @Override
      protected boolean doSelect(Store<Document> store, Document parent,
          Document doc, String filter)
      {
        String label = doc.getLabel().asString().toLowerCase();
        boolean enabled = false;
        if (label.indexOf(filter.toLowerCase()) != -1) {
          enabled = true;
        }
        editButton.setEnabled(enabled);
        return enabled;
      }

      @Override
      protected void onFilter() {
        super.onFilter();
        fileListView.getSelectionModel().select(0, false);
      }
    };

    fileFilterField.setEmptyText("Enter partial file name...");
    fileFilterField.setWidth(200);
    fileFilterField.bind(store);

    VerticalLayoutContainer v = new VerticalLayoutContainer();
    ToolBar toolbar = new ToolBar();
    toolbar.add(fileFilterField);
    addButton(editButton);
    v.add(toolbar, new VerticalLayoutData(1, -1));
    v.add(fileListView, new VerticalLayoutData(1, 1));

    fakeForm.setVisible(false);
    v.add(fakeForm, new VerticalLayoutData(0, 0));

    setWidget(v);
  }
}
